package com.ksm.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.ksm.client.document.presenter.DocumentContainerPresenter.IDocumentContainerView;
import com.ksm.client.presenter.MainPresenter.IMainView;
import com.ksm.client.referential.view.ReferentialContainerView;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.Composite;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.container.FlowLayoutContainer;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.info.Info;

@Singleton
public class MainView extends Composite implements IMainView, IsWidget {

	interface MainViewUiBinder extends UiBinder<Component, MainView> {
	}

	private static MainViewUiBinder uiBinder = GWT.create(MainViewUiBinder.class);

	@Inject
	private IDocumentContainerView documentView;

	@UiField
	FlowLayoutContainer flowLayoutContainer;

	// @UiField(provided = true)
	// ReferentialContainerView referentialView = new
	// ReferentialContainerView();

	@UiField
	String txt = "Slim";

	@UiField
	SimpleContainer documentPanel;

	@UiField
	SimpleContainer referentialPanel;

	@Override
	public Panel getDocumentPanel() {
		return null;// documentPanel;
	}

	@Override
	public void showErrorMessage(String message) {
		Window.alert(message);

	}

	@UiHandler(value = "tabPanel")
	void onSelection(SelectionEvent<Widget> event) {
		TabPanel panel = (TabPanel) event.getSource();
		Widget w = event.getSelectedItem();
		TabItemConfig config = panel.getConfig(w);
		Info.display("Message", "'" + config.getText() + "' Selected");
	}

	public MainView() {
		initWidget(initMyWidget());
	}

	private Widget mywidget;

	public Widget initMyWidget() {
		if (mywidget == null) {
			mywidget = uiBinder.createAndBindUi(this);
			flowLayoutContainer.setScrollMode(ScrollMode.ALWAYS);
		}
		return mywidget;
	}

	@Override
	public Widget asWidget() {
		documentPanel.add(documentView);
		referentialPanel.add(new ReferentialContainerView().asWidget());
		return mywidget;
	}

}
